﻿using System;
using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// 部门Service
    /// </summary>
    public class DepartmentService
    {
        private static readonly IDepartmentDAL dal = DALFactory.DataAccess.createDepartmentDAL();

        //增加部门
        public Hashtable addDepartment(string name)
        {
            Hashtable ht = new Hashtable();
            Department department = dal.getDepartment(name);
            if (department != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "该部门已经存在");
                return ht;
            }
            department = new Department()
            {
                name = name
            };
            try
            {
                dal.saveDepartment(department);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "部门添加成功");
            return ht;
        }

        //删除部门 //暂不实现 //涉及到较多Model

        //修改部门
        public Hashtable modifyDepartment(int id,string name)
        {
            Hashtable ht = new Hashtable();
            Department department = dal.getDepartment(id);
            if (department == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "该部门不存在");
                return ht;
            }
            department.name = name;
            try
            {
                dal.updateDepartment(department);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "部门修改成功");
            return ht;
        }

        //得到部门根据ID
        public Department getDepartmentById(int id)
        {
            return dal.getDepartment(id);
        }

        //得到部门列表
        public IList<Department> getDepartmentList()
        {
            return dal.getDepartment();
        }

    }
}
